package com.xuecheng.orders.interfaces.web;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.xuecheng.orders.infrastructure.config.AlipayConfig;
import com.xuecheng.orders.infrastructure.util.QRCodeUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.xuecheng.base.mq.MqConstant.MQ_EXCHANGE;
import static com.xuecheng.base.mq.MqConstant.MQ_ORDER_KEY;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-09-11  21:37
 */
@RestController
@RequestMapping("/test")
public class TestApi {

    @Value("${alipay.appid}")
    private String appid;

    @Value("${alipay.appPrivateKey}")
    private String appPrivateKey;

    @Value("${alipay.alipayPublicKey}")
    private String alipayPublicKey;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/t1")
    public void test1(HttpServletRequest httpRequest,
                      HttpServletResponse httpResponse) throws IOException, AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, appid, appPrivateKey,
                AlipayConfig.FORMAT, AlipayConfig.CHARSET, alipayPublicKey, AlipayConfig.SIGNTYPE);

//        AlipayTradeCancelRequest alipayTradeCancelRequest = new AlipayTradeCancelRequest();
        AlipayTradePagePayRequest alipayTradeCancelRequest = new AlipayTradePagePayRequest();
        alipayTradeCancelRequest.setReturnUrl("");
        alipayTradeCancelRequest.setNotifyUrl(""); // 在公共参数中设置回跳和通知地址
        JSONObject bizContent = new JSONObject();
//        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", "Page202311150000123");
        bizContent.put("subject", "SS");
        bizContent.put("total_amount", "0.1");
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        alipayTradeCancelRequest.setBizContent(bizContent.toString());

//        AlipayTradeCancelResponse response = alipayClient.pageExecute(alipayTradeCancelRequest);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayTradeCancelRequest);
        System.out.println(response.getBody());
        if (response.isSuccess()){
            System.out.println("成功");
            httpResponse.getWriter().write(response.getBody());// 直接将完整的表单html输出到页面
        } else {
            System.out.println("失败");
            httpResponse.getWriter().write("error");
        }
        httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
        httpResponse.getWriter().flush();
    }

    @GetMapping("/t2")
    public String test2() throws IOException {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        return qrCodeUtil.createQRCode("www.lijunxi.site", 200, 200);
    }

    @GetMapping("/mq")
    public void mqTest(){
        Map<String, String> test = new HashMap<>();
        test.put("1", "2");
        rabbitTemplate.convertAndSend(MQ_EXCHANGE, MQ_ORDER_KEY, test);
        System.out.println("消息发送成功");
    }
}
